﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WorkedTime.Cnsl.ProgramEntities;
using WorkedTime.Cnsl.ProgramEntities.ProgramEnums;

namespace WorkedTime.Cnsl.ProgramEntities
{
    class ProgramActionConfiguration
    {
        private ProgramActionConfiguration() { }
        private static ProgramActionConfiguration _instance;
        public static ProgramActionConfiguration GetInstance()
        {
            if (_instance == null)
            {
                _instance = new ProgramActionConfiguration();
            }
            return _instance;
        }

        private IDictionary<int, string> values;

        public string GetActionName(ProgramOptions programOption)
        {
            if (values == null)
            {
                values = Load();
            }
            return values[(int)programOption];
        }

        private static IDictionary<int, string> Load()
        {
            XDocument xdoc = XDocument.Load(ConfigurationManager.AppSettings["ConfigurationFilesPlace"]);

            return (from item in xdoc.Descendants("ProgramActionConfig")
                    select new
                    {
                        Option = item.Element("Option").Value,
                        Action = item.Element("Action").Value
                    }).ToDictionary(x => (int)((ProgramOptions)Enum.Parse(typeof(ProgramOptions), x.Option)), x => x.Action);
        }
    }
}
